package com.cheer.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheer.common.Page;
import com.cheer.dao.MallSalesroomDao;
import com.cheer.dto.MallSalesroomAddDTO;
import com.cheer.dto.MallSalesroomUpdateDTO;
import com.cheer.entity.MallSalesroomEntity;
import com.cheer.exception.ServiceException;
import com.cheer.service.MallSalesroomService;
import com.cheer.service.MallShopService;
import com.cheer.vo.MallSalesroomInfoVO;
import com.cheer.vo.MallSalesroomListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service("mallSalesroomService")
public class MallSalesroomServiceImpl extends ServiceImpl<MallSalesroomDao, MallSalesroomEntity> implements MallSalesroomService {

    @Autowired
    private MallSalesroomDao mallSalesroomDao;

    @Autowired
    private MallShopService mallShopService;

    @Override
    public Page<MallSalesroomListVO> list(Integer pageNum, Integer pageSize, Long shopId) {
        IPage<MallSalesroomEntity> page = mallSalesroomDao.selectPageByShopId(pageNum, pageSize, shopId);

        List<MallSalesroomListVO> salesroomList = page.getRecords().stream()
                .map(salesroom -> new MallSalesroomListVO()
                        .setId(salesroom.getId())
                        .setName(salesroom.getName())
                        .setWord(salesroom.getWord()))
                .collect(Collectors.toList());

        return new Page<>(page, salesroomList);
    }

    @Override
    public void add(MallSalesroomAddDTO salesroomAddDTO) {
        if (mallShopService.getById(salesroomAddDTO.getShopId()) == null) {
            throw new ServiceException("商户不存在");
        }

        MallSalesroomEntity salesroom = new MallSalesroomEntity()
                .setShopId(salesroomAddDTO.getShopId())
                .setName(salesroomAddDTO.getName())
                .setWord(salesroomAddDTO.getWord());

        mallSalesroomDao.insert(salesroom);
    }

    @Override
    public MallSalesroomInfoVO info(Long id) {
        MallSalesroomEntity salesroom = mallSalesroomDao.selectById(id);
        if(salesroom==null){
            throw new ServiceException("门店不存在");
        }

        return new MallSalesroomInfoVO()
                .setId(salesroom.getId())
                .setName(salesroom.getName())
                .setWord(salesroom.getWord());
    }

    @Override
    public void update(MallSalesroomUpdateDTO salesroomUpdateDTO) {
        if (mallSalesroomDao.selectById(salesroomUpdateDTO.getId()) == null) {
            throw new ServiceException("门店不存在");
        }

        MallSalesroomEntity updateSalesroom = new MallSalesroomEntity()
                .setId(salesroomUpdateDTO.getId())
                .setName(salesroomUpdateDTO.getName())
                .setWord(salesroomUpdateDTO.getWord());

        mallSalesroomDao.updateById(updateSalesroom);
    }

    @Override
    public List<MallSalesroomEntity> listByShopId(Long shopId) {
       return mallSalesroomDao.selectByShopId(shopId);
    }
}